#!groovy

def call(String jenkinsLabel,String projectName) {
    echo projectName
    echo jenkinsLabel
    node("${jenkinsLabel}"){
        def KUTUI_GROUP="https://push.xuthus.cc/group/"
        def DEPLOY_VERSION="SNAPSHOT-1.${BUILD_NUMBER}"
        def ARTI_IPPORT = '10.21.1.105:8083'
        try {
            def PROJECT_NAME = projectName.toLowerCase()
            def NODE_HOSTNAME='host102'

            def HTTP_PORT='3000:3000'
            if(env.BRANCH_NAME == "master"){
                HTTP_PORT='8001:3000'
                DEPLOY_VERSION="RELEASE-1.${BUILD_NUMBER}"
            }
            def GIT_URL = 'https://gitee.com/crowdmall/test7.git'
            //git
            def GIT_CREDENTIALS_ID = 'crowdmall'
            withCredentials([usernamePassword(credentialsId: '1c6eb9ba-7a49-4611-89b3-35feb3c80725', passwordVariable: 'password', usernameVariable: 'username')]) {
                sh "docker login -u ${username} -p ${password} ${ARTI_IPPORT} "
            }
            docker.image(ARTI_IPPORT+'/docker-remote/maven:3.3.3').inside("-v /var/run/docker.sock:/var/run/docker.sock -v /usr/bin/docker:/usr/bin/docker --user root") {
                    stage('Prepare') {
                        echo "${env.BRANCH_NAME}"
                        echo "环境准备"
                    }

                    stage('SCM') {
                        git url: GIT_URL,
                                branch: env.BRANCH_NAME,
                                changelog: true,
                                credentialsId: GIT_CREDENTIALS_ID
                    }
                    stage('add jiraResult') {
                        if (env.BRANCH_NAME == "master") {
                            echo "add jiraResult"
                            //def requirements = getRequirementsIds();
                            //echo "requirements : ${requirements}"

                            //def revisionIds = getRevisionIds();
                            //echo "revisionIds : ${revisionIds}"
                            //rtMaven.deployer.addProperty("project.issues", requirements).addProperty("project.revisionIds", revisionIds)
                        }
                    }
                    //进行测试
                    stage('basic test') {
                        if (env.BRANCH_NAME == "master") {
                            echo "add test step"
                        }
                    }
                    stage('xray scan') {
                        if (env.BRANCH_NAME == "master") {
                            //def xrayConfig = [
                            //        'buildName'  : buildInfo.name,
                            //        'buildNumber': buildInfo.number,
                            //        'failBuild'  : false
                            //]
                            // def xrayResults = artiServer.xrayScan xrayConfig
                            // echo xrayResults as String
                        }
                    }

                    stage('build && push'){
                        withCredentials([usernamePassword(credentialsId: '1c6eb9ba-7a49-4611-89b3-35feb3c80725', passwordVariable: 'password', usernameVariable: 'username')]) {
                            sh '''
                                cd web/h5/
                                docker login -u '''+username+''' -p '''+password+''' '''+ARTI_IPPORT+'''
                                docker build -f ./Dockerfile -t '''+ARTI_IPPORT+'''/docker-local/''' + "${projectName}" + ''':''' + "${DEPLOY_VERSION}" + ''' .
                                docker push '''+ARTI_IPPORT+'''/docker-local/''' + projectName + ''':''' + DEPLOY_VERSION
                        }
                    }
                    stage('deploy'){
                        sh '''
                            export HTTP_PORT='''+HTTP_PORT+'''
                            export NODE_HOSTNAME=''' + "${NODE_HOSTNAME}" + '''
                            export SERVICE_IMAGE='''+ARTI_IPPORT+'''/docker-local/'''+projectName+''':'''+DEPLOY_VERSION+'''
                            docker stack deploy -c jenkins/h5-docker-compose.yml --with-registry-auth '''+projectName+'''-'''+"${env.BRANCH_NAME}"+'''
                        '''
                    }
                }
            withCredentials([string(credentialsId: 'kutui-token', variable: 'KUTUI_KEY')]) {
                echo "完成构建，发qq"
                sh "curl ${KUTUI_GROUP}/$KUTUI_KEY?c=构建状态:SUCCESS:$projectName:${DEPLOY_VERSION}-${env.BRANCH_NAME}"
            }
        }catch(exception1){
            withCredentials([string(credentialsId: 'kutui-token', variable: 'KUTUI_KEY')]) {
                echo "构建异常，发qq"
                sh "curl ${KUTUI_GROUP}/$KUTUI_KEY?c=构建状态:FAILD:$projectName:${DEPLOY_VERSION}-${env.BRANCH_NAME}"
            }
            throw exception1
        }
    }
}